<?php
require('main.php');

class Manager extends Main
{
     function Manager(){
     	
     	parent::Main();
     	$this->lib('message');
     	$this->lib('pagination');
     	$this->database();
     }
     
     
      /**
      * 
      * manager 列表;
      * 
      */ 
     function index(){
     	 $this->my_view("manage/manager/manager",$this->managerList() );
     }
     
     function init_add(){
     	
     	$this->my_view("manage/manager/manager_add");
     	
     }
     
     function add()
     {
     	
     	$result = $this->db->insert('manager',$this->_fetch_form());
     	
        if( $result ) {
			
			$this->message->set("恭喜你,添加管理员成功！");
			$this->my_view('manage/manager/manager_success');
		}
		else {
            $this->message->set("抱歉,添加管理员失败！");
			$this->my_view('manage/manager/manager_add');
		}
     }
     
	function auth() {
    	$this->lib('session');
		if( ! $this->session->userdata('manager_id') ) {
			echo $this->view('manage/login', array(), true);
			exit;
		}
    }
     
     function delete()
     {
     	$id_sql = join(",",$_POST['id']);
     	$result = $this->db->delete("manager","id in($id_sql)");
     	if($result){
     		 $this->message->set("恭喜你,删除成功！");
			 $this->my_view('manage/manager/manager',$this->managerList());
     	}
        else {
            
             $this->message->set("抱歉,删除失败！");
			 $this->my_view('manage/manager/manager',$this->managerList() );
		}
     	
     }
     
     function init_update($id){

     	 $manager_id_row =  $this->db->find("select * from manager where id= $id" ); 
         
         $data = array(
                 'manager_id_row' =>$manager_id_row
         );
     	$this->my_view("manage/manager/manager_update",$data);
     	
    }
     function update(){
     	 $id = $_POST['id'];
     	 $manager_id_row =  $this->db->find("select * from manager where id=".$id ); 
         
         $data = array(
                 'manager_id_row' =>$manager_id_row
         );
         
         $result = $this->db->update("manager",$this->_fetch_form(), 'id='.$id );
		 if($result){
			 $this->message->set("恭喜你,修改成功！");
			 $this->my_view('manage/manager/manager',$this->managerList() ); 
		 }
		 else{
		     $this->message->set("抱歉,修改失败！");
		     $this->my_view('manage/manager/manager_update',$data); 	
			
		 }
     	
     }
     /**
      * 
      * 管理员的增加
      * 
      */
     function _fetch_form()
     {
     	
     	return array(
     	'name'  => $_POST['name'],
     	'pwd'   => $_POST['pwd'],
     	'mail'  => $_POST['mail']
     	);
     	
     	
     }
     
     function managerList(){
     	$total = $this->db->count('manager');
     	$config = array(
     	'total_row' =>$total
     	);
     	$this->pagination->init($config);
     	$manager_list = $this->db->find("select * from manager limit "
     	               .$this->pagination->start_row.",10");
     	               
     	 return array(
     	 'manager_list' => $manager_list
     	 );
     }
     

}
?>